package com.cskaoyan.controller;

import com.cskaoyan.bean.BaseRespVo;
import com.cskaoyan.bean.Topic;
import com.cskaoyan.service.TopicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("admin/topic")
public class TopicController {

    @Autowired
    TopicService topicService;

    @RequestMapping("list")
    public BaseRespVo listTopic(Integer page, Integer limit,
                                String sort, String order,
                                String title, String subtitle) {
        Map<String, Object> topicList = topicService.listTopic(page, limit, sort, order, title, subtitle);
        return BaseRespVo.ok(topicList);
    }

    @PostMapping("create")
    //todo
    public BaseRespVo createTopic(@RequestBody Topic topic) {
        System.out.println(topic);
        topic = topicService.createTopic(topic);
        return BaseRespVo.ok(topic);
    }

    @PostMapping("update")
    //todo
    public BaseRespVo updateTopic(@RequestBody Topic topic){
        Integer update = topicService.updateTopic(topic);
        if (update != 1){
            return BaseRespVo.fail();
        }
        return BaseRespVo.ok(topic);
    }
    @PostMapping("delete")
    //todo
    public BaseRespVo deleteTopic(@RequestBody Topic topic){
        Integer delete = topicService.deleteTopic(topic);
        if (delete != 1){
            return BaseRespVo.fail("系统繁忙");
        }
        return BaseRespVo.ok();
    }
}
